package com.cn.kehong.utils;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;

public class CommonUtil {

    public static String generateRandomNum(String flag) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", flag);
        return uuid;
    }


    public static String generateFileName(String originalFilename) {
        Date date = new Date();
        long time = date.getTime();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = originalFilename.substring(0, originalFilename.lastIndexOf(".")) + "-" + time + suffix;
        return fileName;
    }


    public static String getStrTime(Date date, String format) {
        String strTime = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        if (date != null) {
            try {
                strTime = simpleDateFormat.format(date);
            } catch (Exception e) {

            }
        }
        return strTime;
    }

    public static String outStr(int count) {
        String msg = count == 0 ? Constants.INSERT_NONE : (count < 0 ? Constants.INSERT_ERROR : "");
        return msg;
    }

    public static JsonResponse<String> getResponseData(JsonResponse<String> response, String result) {
        if (StringUtils.isNotEmpty(result) && result.indexOf("id:") == 0) {
            result = result.replace("id:", "");
            response.setResult(result);
            response.setSuccess(true);
            return response;
        }
        if (StringUtils.isNotEmpty(result)) {
            response.setMessage(result);
            response.setSuccess(false);
        } else {
            response.setSuccess(true);
        }
        return response;
    }

    public static int BetweenDay(String startDay, String endDay) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isEmpty(startDay) || StringUtils.isEmpty(endDay)) {
            return -1;
        }

        Date startTime = simpleDateFormat.parse(startDay);
        Date endTime = simpleDateFormat.parse(endDay);
        long dist = (endTime.getTime() - startTime.getTime()) / (1000 * 24 * 3600);
        return Integer.parseInt(String.valueOf(dist));
    }

    public static int BetweenDay(String startDay, Date endDay) throws Exception {
        if (StringUtils.isEmpty(startDay)) {
            return -1;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = simpleDateFormat.parse(startDay);
        long dist = (endDay.getTime() - startTime.getTime()) / (1000 * 24 * 3600);
        return Integer.parseInt(String.valueOf(dist));
    }

    public static boolean isNumber(String str) {
        if (str == null || "".equals(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^(-|\\+)?\\d+(\\.\\d+)?$");
        return pattern.matcher(str).matches();
    }

    //判断整数（int）
    public static boolean isInteger(String str) {
        if (null == str || "".equals(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

    //判断浮点数（double和float）
    public static boolean isDouble(String str) {
        if (null == str || "".equals(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("[+-]?[0-9]+\\.([0-9]+)"); // 之前这里正则表达式错误，现更正
        return pattern.matcher(str).matches();
    }

    public static String convertToWeekday(int dayOfWeek) {
        switch (dayOfWeek) {
            case Calendar.SUNDAY:
                return "周日";
            case Calendar.MONDAY:
                return "周一";
            case Calendar.TUESDAY:
                return "周二";
            case Calendar.WEDNESDAY:
                return "周三";
            case Calendar.THURSDAY:
                return "周四";
            case Calendar.FRIDAY:
                return "周五";
            case Calendar.SATURDAY:
                return "周六";
            default:
                return "";
        }
    }

    public static Map<String,String> getOnDutyAndWhiteDay(){
        return getOnDutyAndWhiteDayByTime(new Date());
    }

    public static Map<String,String> getOnDutyAndWhiteDayByTime(Date date){
        Map<String,String> map = Maps.newHashMap();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dutyDay = simpleDateFormat.format(date);

        String whiteDay = "";
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm");
        String timeDay = simpleDateFormat1.format(date);
        String splits[] =  timeDay.split(":");
        if(Integer.parseInt(splits[0]) >= 8&&Integer.parseInt(splits[0]) < 20){
            whiteDay = "1";
        }else{
            //第二天00：00-8：00夜班也属于前一天夜班
            if((Integer.parseInt(splits[0]) >=0 &&Integer.parseInt(splits[0])< 8)){
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DATE, -1);
                Date previousDay = calendar.getTime();
                dutyDay = simpleDateFormat.format(previousDay);
            }
            whiteDay = "2";
        }
        map.put("dutyDay",dutyDay);
        map.put("whiteDay",whiteDay);
        return map;
    }

}
